package cn.com.lms.business.examRewrite.service;

import cn.com.lms.business.examRewrite.persistence.entity.ExerciseEmployeeReWriteVo;
import cn.com.lms.business.examRewrite.persistence.entity.ExerciseQuestionReWrite;
import cn.com.lms.business.examRewrite.persistence.entity.ExerciseReWrite;
import cn.com.lms.business.examRewrite.persistence.repository.ExerciseEmployeeReWriteVoDao;
import com.bnzj.core.persistence.BaseService;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ExerciseEmployeeReWriteVoService extends BaseService<ExerciseEmployeeReWriteVoDao, ExerciseEmployeeReWriteVo,Long> {

    public List<ExerciseEmployeeReWriteVo> getEmployeeById(Long id) {
        return repository.getEmployeeById(id);
    }

    /**
     * 返回一个数据库没有数据的EmployeeID
     * @param exerciseReWrite 前端传过来的实体类
     * @return
     */
    public List<ExerciseEmployeeReWriteVo> getUniqueEmployeeId(ExerciseReWrite exerciseReWrite) {
        List<ExerciseEmployeeReWriteVo> exerciseEmployeeReWriteVos = new ArrayList<>();
        // 去数据库查查数据
        List<ExerciseEmployeeReWriteVo> exerciseEmployeeList1 = repository.getDataById(exerciseReWrite.getId());

       // 前端
        List<ExerciseEmployeeReWriteVo> exerciseEmployeeList = exerciseReWrite.getExerciseEmployeeList();
        for (ExerciseEmployeeReWriteVo exerciseEmployeeReWriteVo : exerciseEmployeeList) {
            Boolean result = false;
            for (ExerciseEmployeeReWriteVo employeeReWriteVo : exerciseEmployeeList1) {
                if (exerciseEmployeeReWriteVo.getEmployeeId().equals(employeeReWriteVo.getEmployeeId())){
                    result = true;
                    break;
                }
            }
            if (!result){
                ExerciseEmployeeReWriteVo exerciseEmployeeReWriteVo1 = new ExerciseEmployeeReWriteVo();
                exerciseEmployeeReWriteVo1.setEmployeeId(exerciseEmployeeReWriteVo.getEmployeeId());
                exerciseEmployeeReWriteVos.add(exerciseEmployeeReWriteVo1);
            }

        }
        return exerciseEmployeeReWriteVos;
    }

    /**
     * 批量保存employee数据
     * @param exerciseReWriteData
     */
    public void batchSave(ExerciseReWrite exerciseReWriteData) {
        List<ExerciseEmployeeReWriteVo> exerciseEmployeeList = exerciseReWriteData.getExerciseEmployeeList();
        if (exerciseEmployeeList!=null && exerciseEmployeeList.size()>0){
            exerciseEmployeeList.stream().forEach(exerciseEmployeeReWriteVo -> {
                exerciseEmployeeReWriteVo.setExerciseId(exerciseReWriteData.getId());
                exerciseEmployeeReWriteVo.setIsDelete(false);
                exerciseEmployeeReWriteVo.setCreator(exerciseReWriteData.getCreator());
                exerciseEmployeeReWriteVo.setCreateTime(new Date());
                exerciseEmployeeReWriteVo.setCreatorName(exerciseReWriteData.getCreatorName());
                save(exerciseEmployeeReWriteVo);
            });
        }
    }

    /**
     * 通过exercise_id获取employeeId
     * @param id
     * @return
     */
    public List<Long> getEmployeexByExerciseId(Long id) {
        return repository.getEmployeexByExerciseId(id);
    }

    /**
     * 通过exerciseID删除
     * @param result
     * @param id
     */
    public void deleteSByExerciseId(boolean result, List<Long> id) {
        repository.deleteSByExerciseId(result,id);

    }
}
